OPC Studio User's Guide and Reference
Examples - OPC UA Complex Data - Subscribe to data change
View with Navigation Tools

The example below shows how to subscribe to OPC UA complex data and display the incoming values.

.NET

// Shows how to subscribe to complex data with OPC UA Complex Data plug-in.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples.ComplexData._EasyUAClient
{
    class SubscribeDataChange
    {
        public static void Main1()
        {
            // Define which server and node we will work with.
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"
            UANodeDescriptor nodeDescriptor =
                "nsu=http://test.org/UA/Data/ ;i=10867"; // [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue

            // Instantiate the client object and hook the event handler.
            var client = new EasyUAClient();
            client.DataChangeNotification += client_DataChangeNotification;

            // Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just 
            // the data returned is different.
            Console.WriteLine("Subscribing...");
            client.SubscribeDataChange(endpointDescriptor, nodeDescriptor, samplingInterval:1000);

            Console.WriteLine("Processing data change events for 20 seconds...");
            System.Threading.Thread.Sleep(20 * 1000);

            Console.WriteLine("Unsubscribing...");
            client.UnsubscribeAllMonitoredItems();

            Console.WriteLine("Waiting for 5 seconds...");
            System.Threading.Thread.Sleep(5 * 1000);

            // Unhook the event handler.
            client.DataChangeNotification -= client_DataChangeNotification;


            // Example output:
            //
            //Subscribing...
            //Processing data change events for 20 seconds...
            //(ScalarValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ScalarValueDataType) structured
            //(ScalarValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ScalarValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ScalarValueDataType) structured
            //(ScalarValueDataType) structured
            //(ScalarValueDataType) structured
            //Unsubscribing...
            //Waiting for 5 seconds...
        }

        static void client_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
        {
            // Display value
            if (e.Succeeded)
                Console.WriteLine("Value: {0}", e.AttributeData.Value);
            else
                Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief);

            // For processing the internals of the data, refer to examples for GenericData and DataType classes.
        }
    }
}

COM

// Shows how to subscribe to complex data with OPC UA Complex Data plug-in.
//
// Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

type
  TClientEventHandlers33 = class
    procedure OnDataChangeNotification(
      ASender: TObject;
      sender: OleVariant;
      const eventArgs: _EasyUADataChangeNotificationEventArgs);
  end;

procedure TClientEventHandlers33.OnDataChangeNotification(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyUADataChangeNotificationEventArgs);
begin
  // Display value
  if eventArgs.Succeeded then
      WriteLn('Value: ', eventArgs.AttributeData.Value.ToString)
  else
      WriteLn('*** Failure: ', eventArgs.ErrorMessageBrief);

  // For processing the internals of the data, refer to examples for GenericData and DataType classes.
end;

class procedure SubscribeDataChange.Main;
var
  Client: TEasyUAClient;
  ClientEventHandlers: TClientEventHandlers33;
  EndpointDescriptor: string;
  NodeDescriptor: string;
begin
  // Define which server and node we will work with.
  EndpointDescriptor := 
    //'http://opcua.demo-this.com:51211/UA/SampleServer';
    //'https://opcua.demo-this.com:51212/UA/SampleServer/';
    'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer';
  NodeDescriptor := 'nsu=http://test.org/UA/Data/ ;i=10867';  // [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue

  // Instantiate the client object and hook events
  Client := TEasyUAClient.Create(nil);
  ClientEventHandlers := TClientEventHandlers33.Create;
  Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification;

  // Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just
  // the data returned is different.
  WriteLn('Subscribing...');
  Client.SubscribeDataChange(EndpointDescriptor, NodeDescriptor, 1000);

  WriteLn('Processing data change events for 20 seconds...');
  PumpSleep(20*1000);

  WriteLn('Unsubscribing...');
  Client.UnsubscribeAllMonitoredItems;

  WriteLn('Waiting for 5 seconds...');
  PumpSleep(5*1000);

  WriteLn('Finished.');
  FreeAndNil(Client);
  FreeAndNil(ClientEventHandlers);

  // Example output:
  //
  //Subscribing...
  //Processing data change events for 20 seconds...
  //(ScalarValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ScalarValueDataType) structured
  //(ScalarValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ScalarValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ScalarValueDataType) structured
  //(ScalarValueDataType) structured
  //(ScalarValueDataType) structured
  //Unsubscribing...
  //Waiting for 5 seconds...
end;

 

See Also

Conceptual

Examples - OPC Unified Architecture